package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.pojo.user.*;
import com.dongdongshop.service.user.AddressService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Eayon
 * @Date 2020/3/14 10:40
 */
@Service
@Component
public class AddressServiceImpl implements AddressService {

    @Resource
    private TbAddressMapper addressMapper;

    @Resource
    private TbProvincesMapper provincesMapper;

    @Resource
    private TbCitiesMapper citiesMapper;

    @Resource
    private TbAreasMapper areasMapper;

    /**
     * 根据用户id查询相关联的地址信息
     * @param id
     * @return
     */
    @Override
    public List<TbAddress> getAddressByUserId(Long id) {
        TbAddressExample example = new TbAddressExample();
        TbAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id.toString());
        return addressMapper.selectByExample(example);
    }

    /**
     * 获取所有省信息
     * @return
     */
    @Override
    public List<TbProvinces> findProvincesAll() {
        return provincesMapper.selectByExample(null);
    }

    /**
     * 根据省份id获取市级信息
     * @param provinceid
     * @return
     */
    @Override
    public List<TbCities> getCitiesByProvinceid(String provinceid) {
        TbCitiesExample example = new TbCitiesExample();
        TbCitiesExample.Criteria criteria = example.createCriteria();
        criteria.andProvinceidEqualTo(provinceid);
        return citiesMapper.selectByExample(example);
    }

    /**
     * 根据市级id查询县区信息
     * @param cityid
     * @return
     */
    @Override
    public List<TbAreas> getAreasByCityid(String cityid) {
        TbAreasExample example = new TbAreasExample();
        TbAreasExample.Criteria criteria = example.createCriteria();
        criteria.andCityidEqualTo(cityid);
        return areasMapper.selectByExample(example);
    }

    /**
     * 增加用户收货地址
     * @param address
     */
    @Override
    public void addAddress(TbAddress address) {
        addressMapper.insertSelective(address);
    }

    @Override
    public TbAddress getAddressById(Long id) {
        return addressMapper.selectByPrimaryKey(id);
    }
}
